import 'nprogress/nprogress.css'; // 进度条样式
import type { App } from 'vue';
import { createRouter, createWebHashHistory } from 'vue-router';
import baseRouters from './base';
import { whiteNameList } from './constant';
import { createRouterGuards } from './router-guards';

const router = createRouter({
  history: createWebHashHistory(''),
  routes: baseRouters
});

// reset router
export function resetRouter() {
  router.getRoutes().forEach(route => {
    const { name } = route;
    if (name && !whiteNameList.some(n => n === name)) {
      router.hasRoute(name) && router.removeRoute(name);
    }
  });
}

export async function setupRouter(app: App) {
  // 路由守卫
  createRouterGuards(router, whiteNameList);

  app.use(router);

  // 路由准备就绪后挂APP实例
  await router.isReady();
}

export default router;
